package logic

import (
	"context"
	"fmt"
	"go-zero-demo/product/model"
	"strings"

	"go-zero-demo/product/api/internal/svc"
	"go-zero-demo/product/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateLogic(ctx context.Context, svcCtx *svc.ServiceContext) CreateLogic {
	return CreateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateLogic) Create(req types.CreateRequest) (resp *types.CreateResponse, err error) {
	if len(strings.TrimSpace(req.Name)) == 0 || len(strings.TrimSpace(req.Desc)) == 0 {
		fmt.Println("参数错误！")
	}
	fmt.Println(req)
	data := &model.Product{
		Name:   req.Name,
		Desc:   req.Desc,
		Stock:  req.Stock,
		Amount: req.Amount,
		Status: req.Status,
	}
	affected, err := l.svcCtx.ProductModel.Insert(data)
	fmt.Println(affected)
	return &types.CreateResponse{
		Id: affected,
	}, nil
}
